package br.com.skpharma.dao;
import java.util.ArrayList;

import br.com.skpharma.db.DB;
import br.com.skpharma.vo.StatusVO;



public class DAOStatus {
	private DB db = new DB("jdbc:postgresql://localhost:5432/skpharma","postgres","postgres");
	private StatusVO statusVO;

	public DAOStatus(){
		statusVO = new StatusVO();
	}
	
	public StatusVO getStatusVO() {
		return statusVO;
	}

	public void setStatusVO(StatusVO statusVO) {
		this.statusVO = statusVO;
	}
	
	public void salvar(){
		if(db.connect()){
			db.update("insert into status (ds_status) values ('" + statusVO.getDs() + "')");			
			db.disconnect();
		}		
	}

	public void excluir(){
		if(db.connect()){
			db.update("delete from status where cod_status = " + statusVO.getCod());			
			db.disconnect();
		}		
	}
	
	public void editar(){		
		if(db.connect()){
			db.update("update status set ds_status ='"+ statusVO.getDs() +"' where cod_status = "+ statusVO.getCod());			
			db.disconnect();
		}		
	}

	public void carregar(int codigo){		
		if(db.connect()){
			db.select("select cod_status, ds_status from status where cod_status=" + codigo);
			if(db.moveNext()){
				statusVO.setCod(db.getInt("cod_status"));
				statusVO.setDs(db.getString("ds_status"));
			}
			db.disconnect();
		}		
	}
	
	public ArrayList obterLista(){
		ArrayList lista = new ArrayList();
		StatusVO status;		
		if(db.connect()){
			db.select("select cod_status, ds_status from status order by cod_status");
			while(db.moveNext()){
				status = new StatusVO(db.getInt("cod_status"),db.getString("ds_status"));
				lista.add(status);
			}
			db.disconnect();
		}		
		return lista;
	}
	
	
	
}
